/*
 * 创建日期 2005-8-4
 *
 */
package wangjg.commons.io.file;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @author wangjg
 * 排序类
 */
public class Order {
    
	/**
	 * 按修改日期排序文件
	 * 
	 * @param files
	 * @return
	 */
	public static File[] byDate(File[] files,boolean asc) {
		int size = files.length;
		HashMap<Object,File> fileMap = new HashMap<Object,File>();
		for (int i = 0; i < size; i++) {
			long time = files[i].lastModified();
			fileMap.put(Long.valueOf(time), files[i]);
		}
		return Order.order(fileMap,asc);
	}

	/**
	 * 按键名排序map
	 * @param map
	 * @return
	 */
	private static File[] order(HashMap<Object,File> map,boolean asc) {
		Object[] keys = map.keySet().toArray();
		Arrays.sort(keys);
		File[] files = new File[keys.length];
		int n = keys.length - 1;
		for(int i=0;i<=n;i++){
			if(asc){
				files[i] = map.get(keys[i]);
			}else{
				files[i] = map.get(keys[n - i]);
			}
		}
		return files;
	}

	public static void main(String args[]) {
		System.out.println(new File("f:\\temp\\").lastModified());
	}
}
